package com.kk.xhr.kafka.producer;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.List;

/**
 * IKafkaProducer
 *
 * @author liujingcheng
 * @version 1.0, 2020/7/21 09:55
 * @since 1.0.2
 */
public interface IKafkaProducer {

    /**
     * 发送单个消息
     *
     * @param topic   topic
     * @param message content
     * @return 发送结果
     */
    ListenableFuture<SendResult<String, String>> sendMessage(String topic, String message);

    /**
     * 发送相同{@code key}的消息到同一个分区
     *
     * <p>若key相同，经过hash计算后，会将相同key的消息映射到同一个分区
     *
     * @param topic   topic
     * @param key     key
     * @param message content
     * @return 发送结果
     */
    ListenableFuture<SendResult<String, String>> sendMessage(String topic, String key, String message);

    /**
     * 批量发送消息
     *
     * @param topicContents {topic, content} list
     */
    void sendMessages(List<Pair<String, String>> topicContents);

    /**
     * 事务性批量发送消息
     *
     * @param topicContents {topic, content} list
     */
    void sendMessagesTransaction(List<Pair<String, String>> topicContents);
}
